<?php
class crypto{
	/* validates a recaptcha that the user had to fill in */
	public static function validateRecaptcha($userTypedValue){
		$result = false;

		$validationUrl = "https://www.google.com/recaptcha/api/siteverify"
						."?secret=".urlencode(CONFIG_RECAPTCHA_SECRET)
						."&response=".urlencode($userTypedValue)
						."&remoteip=".$_SERVER["REMOTE_ADDR"];

		// now load the result from the google server
		$apiResult = json_decode(file_get_contents($validationUrl));

		// check if the json result indicator is true
		if($apiResult->success==true){
			$result = true;
		}

		return $result;
	}

	/* encrypts a plain text with the key provided */
	public static function encryptText($plainText, $secretKey){
    	return rtrim(
        	base64_encode(
            	mcrypt_encrypt(
                	MCRYPT_RIJNDAEL_256,
                	$secretKey, $plainText, 
                	MCRYPT_MODE_ECB, 
                	mcrypt_create_iv(
                    	mcrypt_get_iv_size(
                        	MCRYPT_RIJNDAEL_256, 
                        	MCRYPT_MODE_ECB
                    	), 
                    	MCRYPT_RAND)
                	)
            	), "\0"
        	);
	}

	/* decrypts a cipher text with the key provided */
	public static function decryptText($cipherText,$secretKey){
		return rtrim(
	        mcrypt_decrypt(
	            MCRYPT_RIJNDAEL_256, 
	            $secretKey, 
	            base64_decode($cipherText), 
	            MCRYPT_MODE_ECB,
	            mcrypt_create_iv(
	                mcrypt_get_iv_size(
	                    MCRYPT_RIJNDAEL_256,
	                    MCRYPT_MODE_ECB
	                ), 
	                MCRYPT_RAND
	            )
	        ), "\0"
    	);
	}
}
?>